/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.flowable.ui.modeler.rest.app;

import java.util.ArrayList;
import java.util.List;

import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.RemoteGroup;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.daffodil.core.entity.Query;
import com.daffodil.flowable.service.IFlowableAccountService;
import com.daffodil.system.entity.SysRole;
import com.daffodil.util.StringUtils;

/**
 * 该部分重写为了修改获取分组
 * @author yweijian
 * @date 2021年11月15日
 * @version 1.0
 * @description
 */
@RestController
@RequestMapping("/app")
public class EditorGroupsResource {
    
    @Autowired
    private IFlowableAccountService accountService;

    @RequestMapping(value = "/rest/editor-groups", method = RequestMethod.GET)
    public ResultListDataRepresentation getGroups(@RequestParam(required = false, value = "filter") String filter) {
        Query<SysRole> query = new Query<SysRole>();
        SysRole sysRole = new SysRole();
        sysRole.setRoleName(filter);
        query.setEntity(sysRole);
        List<SysRole> list = accountService.selectRoleList(query);
        List<GroupRepresentation> groupRepresentations = new ArrayList<GroupRepresentation>();
        if(StringUtils.isNotEmpty(list)){
            for(SysRole role : list){
                //设置用户组即SysRole->group
                RemoteGroup group = new RemoteGroup();
                group.setId(role.getId());
                group.setName(role.getRoleName());
                group.setType(role.getRoleKey());
                groupRepresentations.add(new GroupRepresentation(group));
            }
        }
        return new ResultListDataRepresentation(groupRepresentations);
    }
}
